package com.qianfeng.bob.xiexiaobo.fragment;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.qianfeng.bob.xiexiaobo.R;
import com.qianfeng.bob.xiexiaobo.application.MyApplication;
import com.qianfeng.bob.xiexiaobo.cache.MyCache;

/**
 * Created by Bob on 2015/12/16.
 * 这个类是Fragment的基类
 */
public class BaseFragment extends Fragment {
    /**
     * 子类进行网络请求的请求队列
     */
    protected RequestQueue requestQueue;
    private MyCache mLruCache;
    private Dialog mDialog;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        if (mDialog==null){
            mDialog = new ProgressDialog(getActivity());
            mDialog.setTitle("正在为您加载数据...");
        }

        if (requestQueue==null){
            requestQueue = ((MyApplication) getActivity().getApplication()).getRequestQueue();
        }
        if (mLruCache == null){
            mLruCache = ((MyApplication) getActivity().getApplication()).getMyCache();
        }
    }

    /**
     *  给子类adapter加载数据用的
     * @param url
     * @param callBack
     */
    protected  void getString(String url, final StringCallBack callBack){
        mDialog.show();
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                mDialog.cancel();
                if (callBack!=null){
                    callBack.getString(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mDialog.cancel();
                if (callBack!=null){
                    callBack.getStringField();
                }
            }
        });
        request.setTag(this.getClass().getSimpleName());
        requestQueue.add(request);

    }
    public interface StringCallBack{
         void getString(String content);
         void getStringField();
    }

    /**
     * 给子类adapter加载图片用的
     * @param imageView  图片的路径放到了ImageView的getTag() 里面，所以要使用这个方法加载图片的话，就要先setTag();
     */
    protected    void loadImage(ImageView imageView){
        String url = ((String) imageView.getTag());
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.bottom_comment_input,R.mipmap.bottom_comment_input);
        ImageLoader loader = new ImageLoader(requestQueue,mLruCache);
        loader.get(url,listener);
    }

    /**
     * 销毁dialog和取消网络请求
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDialog!=null){
            mDialog.cancel();
        }
        if (requestQueue!=null){
            requestQueue.cancelAll(this.getClass().getSimpleName());
        }
    }
}
